package com.linxiao.call.service.impl;


import java.util.List;
import com.linxiao.call.domain.CrmWebcallCdr;
import com.linxiao.call.domain.CrmWebcallFile;
import com.linxiao.call.domain.CrmWebcallFileTel;
import com.linxiao.call.mapper.CrmWebcallCdrMapper;
import com.linxiao.call.service.ICrmWebcallCdrService;
import com.linxiao.call.service.ICrmWebcallFileService;
import com.linxiao.call.service.ICrmWebcallFileTelService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.*;
import com.ruoyi.system.manager.AdminManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * webCall语音通知Service业务层处理
 *
 * @author qiqi
 * @date 2022-06-10
 */
@Service
public class CrmWebcallCdrServiceImpl implements ICrmWebcallCdrService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private CrmWebcallCdrMapper crmWebcallCdrMapper;
    @Resource
    private ICrmWebcallFileTelService crmWebcallFileTelService;
    @Resource
    private ICrmWebcallFileService crmWebcallFileService;
    @Resource
    private AdminManager adminManager;

    /**
     * 查询webCall语音通知
     *
     * @param id webCall语音通知主键
     * @return webCall语音通知
     */
    @Override
    public CrmWebcallCdr selectCrmWebcallCdrById(Long id) {
        return crmWebcallCdrMapper.selectCrmWebcallCdrById(id);
    }

    /**
     * 查询webCall语音通知列表
     *
     * @param crmWebcallCdr webCall语音通知
     * @return webCall语音通知
     */
    @Override
    @DataScope(deptAlias="")
    public List<CrmWebcallCdr> selectCrmWebcallCdrList(CrmWebcallCdr crmWebcallCdr) {
        LoginUser user = SecurityUtils.getLoginUser();
        // 如果是管理岗位，取所管理的部门，如果不是管理岗位，取个人外呼记录
        if (!adminManager.loginUserIsLeader(user)) {
            crmWebcallCdr.setUserId(user.getUserId());
        }
        // 查询数据库
        PageUtils.startPage();
        List<CrmWebcallCdr> crmWebcallCdrs = crmWebcallCdrMapper.selectCrmWebcallCdrList(crmWebcallCdr);
        return crmWebcallCdrs;
    }

    /**
     * 新增webCall语音通知
     *
     * @param crmWebcallCdr webCall语音通知
     * @return 结果
     */
    @Override
    public int insertCrmWebcallCdr(CrmWebcallCdr crmWebcallCdr) {
        try {
            if (StringUtils.isNotEmpty(crmWebcallCdr.getCdrCustomerNumber())) {
                String cdrCustomerNumber = crmWebcallCdr.getCdrCustomerNumber();
                // 脱敏手机号
                crmWebcallCdr.setMobileHidden(EncryptContent.replaceAction(cdrCustomerNumber));

                crmWebcallCdr.setCreateTime(DateUtils.getNowDate());
                crmWebcallCdr.setId(Long.valueOf(IdSeqGenerator.getInstance().getId()));


                if(StringUtils.isNotEmpty(crmWebcallCdr.getFileId())){
                    try{//NOSONAR
                        CrmWebcallFileTel crmWebcallFileTel = new CrmWebcallFileTel();
                        crmWebcallFileTel.setFileId(Long.valueOf(crmWebcallCdr.getFileId()));
                        crmWebcallFileTel.setTel(crmWebcallCdr.getCdrCustomerNumber());
                        crmWebcallFileTel.setAnswerStatus(crmWebcallCdr.getCdrStatus().intValue());

                        crmWebcallFileTelService.updateCrmWebcallFileTelByFileId(crmWebcallFileTel);
                        // 根据批次id获取部门记录
                        CrmWebcallFile webcallFile = crmWebcallFileService.selectCrmWebcallFileById(Long.parseLong(crmWebcallCdr.getFileId()));
                        if(webcallFile != null){
                            crmWebcallCdr.setDeptId(webcallFile.getDeptId());
                            crmWebcallCdr.setUserId(webcallFile.getUserId());
                        }
                    }catch (Exception e){
                        log.error("CrmWebcallCdrServiceImpl.insertCrmWebcallCdr error = {}",e);
                    }

                }
                return crmWebcallCdrMapper.insertCrmWebcallCdr(crmWebcallCdr);
            }
        } catch (Exception e) {
            log.error("新增外呼通话记录失败 e={}", e);
        }
        return 0;
    }

    /**
     * 修改webCall语音通知
     *
     * @param crmWebcallCdr webCall语音通知
     * @return 结果
     */
    @Override
    public int updateCrmWebcallCdr(CrmWebcallCdr crmWebcallCdr) {
        return crmWebcallCdrMapper.updateCrmWebcallCdr(crmWebcallCdr);
    }

    /**
     * 批量删除webCall语音通知
     *
     * @param ids 需要删除的webCall语音通知主键
     * @return 结果
     */
    @Override
    public int deleteCrmWebcallCdrByIds(Long[] ids) {
        return crmWebcallCdrMapper.deleteCrmWebcallCdrByIds(ids);
    }

    /**
     * 删除webCall语音通知信息
     *
     * @param id webCall语音通知主键
     * @return 结果
     */
    @Override
    public int deleteCrmWebcallCdrById(Long id) {
        return crmWebcallCdrMapper.deleteCrmWebcallCdrById(id);
    }
}
